package com.demo.mvc.controller;

import com.demo.api.user.domain.TestUser;
import com.demo.api.user.service.UserService;
import com.demo.common.annotation.Clog;
import com.demo.mvc.annotation.TestAno;
import com.demo.mvc.service.TestService;
import io.swagger.annotations.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;


@RestController
@Slf4j
@Api(value = "测试")
public class Test01 {
    @Autowired
    private TestService service;

    @Reference(version = "1.0.0", check = false)
    private UserService userService;

    @ApiOperation("foo测试")
    @ApiImplicitParam(name = "t", value = "时间戳", required = false, dataType = "Long")
    @GetMapping("/foo")
    public String apiFoo(@RequestParam(required = false)  Long t) throws Exception {
        if (t == null) {
            t = System.currentTimeMillis();
        }
        service.test();
        return service.hello(t);
    }

    @ApiOperation("baz测试")
    @GetMapping("/baz/{name}")
    @TestAno
    public String apiBaz(@PathVariable("name") String name) {
        Test003 test003 = new Test003();
        test003.setV1("123");

        return service.helloAnother(name);
    }

    @ApiOperation("log测试")
    @GetMapping("/log")
    public void printLog(){
        log.info("---------info-----------");
        log.debug("--------debug----------");
        log.trace("--------trace-----------");
        log.warn("----------warn-----------");
        log.error("----------error---------");
    }

    @ApiOperation("save测试")
    @ApiImplicitParam(name = "test004", value = "测试004实体类", required = true, dataType = "Test004")
    @GetMapping("/save")
    @Clog(descrption = "Test Save")
    public Test004 save(Test004 test004){
      log.info("------ 接收到 save 请求 ----");
      test004.val001 = "Test#Val001";
      test004.val002 = "Test#Val002";
      log.info("------ 接收到 save 处理完毕 ----");
      return test004;
    }

//  @ApiOperation("测试异常")
//  @GetMapping("/error")
//  public void testError(){
//    log.info("------ 接收到 error 请求 ----");
//    int i = 1/0;
//  }

  @ApiOperation("save测试")
  @ApiImplicitParam(name = "addUser", value = "调用dubbo测试", required = true, dataType = "TestUser")
  @PostMapping("/transaction/user/")
  public String addUser(@NotNull @RequestBody TestUser testUser){
      log.info("接收到addUser请求，参数{}", testUser);
      try {
        userService.addUser(testUser);
      } catch (Exception e){
        log.error("{}",e);
      }
      return "Okay";
  }

    @Data
    class Test003 {
        @TestAno
        String v1;
        String v2;
    }

  @Data
  @ApiModel("Test004实体类")
  class Test004 {
    @ApiModelProperty("值01")
    String val001;
    @ApiModelProperty("值02")
    String val002;
  }

}
